package cn.anxingongyou.anxin.controller;

import cn.anxingongyou.anxin.entity.*;
import cn.anxingongyou.anxin.service.AdminService;
import cn.anxingongyou.anxin.service.LoginService;
import cn.anxingongyou.anxin.utils.Result;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
public class LoginController {

    protected static final Logger logger = LoggerFactory.getLogger(LoginController.class);

    private final LoginService loginService;
    private final AdminService adminService;
    @Autowired
    public LoginController(LoginService loginService, AdminService adminService) {
        this.loginService = loginService;
        this.adminService = adminService;
    }


    @Value("${weixin.appid}")
    private String appID;
    @Value("${weixin.appsecret}")
    private String appSecret;

    @ApiOperation(value = "api用户登录")
    @ApiResponses({
            @ApiResponse(code = 200,message = "登录成功"),
            @ApiResponse(code = 406,message = "验证失败"),
            @ApiResponse(code = 500,message = "登录失败")
    })
    @RequestMapping(value = "/api/wx/login",method = RequestMethod.POST)
    @ResponseBody
    public Map login(
            @RequestBody @ApiParam(value = "用户数据",required = true) Map<String,Object> param
    ) throws Exception {
        logger.info("param:{}",param.toString());
        ObjectMapper mapper = new ObjectMapper();
        PhoneEncryptedData phoneEncryptedData =  mapper.convertValue(param.get("phoneEncryptedData"),PhoneEncryptedData.class);
        UserWorkerInfo userWorkerInfo = mapper.convertValue(param.get("userWorkerInfo"),UserWorkerInfo.class);
        OpenIdJson openIdJson = loginService.getOpenID(phoneEncryptedData,appID,appSecret);
        logger.info("openIdJson:{}",openIdJson.toString());
        Map<String, Object> phoneMap = loginService.getPhoneMap(phoneEncryptedData, openIdJson);
        logger.info("phoneMap:{}",phoneMap.toString());
        // 登录
        Map<String, Object> map = loginService.addOrGetUser(phoneMap, openIdJson, userWorkerInfo);
        return map;
    }


    @ApiOperation(value = "api管理员登录")
    @ApiResponses({
            @ApiResponse(code = 200,message = "登录成功"),
            @ApiResponse(code = 406,message = "验证失败"),
            @ApiResponse(code = 500,message = "登录失败")
    })
    @RequestMapping(value = "/api/admin/login",method = RequestMethod.POST)
    @ResponseBody
    public Map adminLogin(
            @RequestBody @ApiParam(value = "管理员用户名密码",required = true)Admin admin
            ) throws Exception {
        // 登录
        admin = adminService.selectOne(admin);
        if(admin != null){
            return Result.<Admin>instance(204,"登录成功",admin).toJsonMap();
        } else {
            return Result.instance(500,"用户名或密码不正确").toJsonMap();
        }
    }

    @ApiOperation(value = "api获取用户信息")
    @ApiResponses({
            @ApiResponse(code = 200,message = "登录成功"),
            @ApiResponse(code = 500,message = "登录失败")
    })
    @RequestMapping(value = "/api/getuserinfo/{id}",method = RequestMethod.GET)
    @ResponseBody
    public Map getUserInfo(
            @PathVariable("id") @ApiParam(value = "用户主键",required = true) Long id
    ) throws Exception {
        logger.info("id:{}",id.toString());
        // 获取用户信息
        Map<String, Object> map = loginService.getUserInfo(id);
        return map;
    }



}
